package com.tikou.ui.view.circleview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Tikou on 2018/1/3 09:37.
 * 作者：wangwei
 * 邮箱：ww@winning.com.cn
 * 说明：圆环view
 * 参考：https://juejin.im/post/5a31df7c6fb9a045031018b3
 */

public class CircleBarView extends View{

    private Paint mPaint; // 参考矩形
    private Paint cPaint; // 圆环
    private Paint bPaint; // 背景
    private float sweepAngle;
    private CircleBarAnim anim;
    private int defaultSize;
    private RectF rectF;
    private float barWidth;
    public CircleBarView(Context context) {
        this(context,null);
    }

    public CircleBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        defaultSize = 200;
        barWidth = 10;
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(barWidth);
        cPaint = new Paint();
        cPaint.setStyle(Paint.Style.STROKE);
        cPaint.setColor(Color.BLUE);
        cPaint.setAntiAlias(true);
        cPaint.setStrokeWidth(barWidth);
        anim = new CircleBarAnim();
        bPaint = new Paint();
        bPaint.setAntiAlias(true);
        bPaint.setColor(Color.GRAY);
        bPaint.setStyle(Paint.Style.STROKE);
        bPaint.setStrokeWidth(barWidth);
        anim.bindView(this);
        rectF = new RectF(0,0,200,200);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.drawArc(rectF,0,360,false,bPaint);
        canvas.drawArc(rectF,0,sweepAngle,false,cPaint);
        canvas.drawRect(rectF,mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = measureSize(defaultSize,widthMeasureSpec);
        int height = measureSize(defaultSize,heightMeasureSpec);
        int min = Math.min(width,height);
        setMeasuredDimension(min,min);
        if (min>=barWidth*2){
            // rectF.set();
        }

    }
    private int measureSize(int defaultSize,int measureSpec) {
        int result = defaultSize;
        int specMode = View.MeasureSpec.getMode(measureSpec);
        int specSize = View.MeasureSpec.getSize(measureSpec);

        if (specMode == View.MeasureSpec.EXACTLY) {
            result = specSize;
        } else if (specMode == View.MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
        return result;
    }
    /**
     * @param time 设置动画时间
     */
    public void setProgressTime(int time){
        anim.setDuration(time);
    }

    /**
     *  开始动画
     */
    public void start(){
        startAnimation(anim);
    }

    public float getSweepAngle() {
        return sweepAngle;
    }

    /**
     * @param sweepAngle 动画动态刷新
     */
    public void setSweepAngle(float sweepAngle) {
        this.sweepAngle = sweepAngle;
        postInvalidate();
    }
}
